/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <cstdio>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <vector>
#include <mutex>
#include <string.h>
#include <sys/wait.h>
#include "It_process_plimits.h"

static int pidGroup[10] = {0};
static int g_index = 0;

static int pidWrite(const char *filepath, char *buf)
{
    int fd = open(filepath, O_WRONLY);
    size_t twd = write(fd, buf, (strlen(buf) + 1));
    close(fd);
    return twd;
}

static int pidForkOne(const char *filepath, int index)
{
    int ret = 0;
    char writeBuf[4];
    pid_t fpid = fork();
    if (fpid == 0) {  // children proc
        sleep(5);
        exit(0);
    } else if (fpid > 0) {
        pidGroup[index] = fpid;
        (void)memset(writeBuf, 0, sizeof(writeBuf));
        sprintf_s(writeBuf, sizeof(writeBuf), "%d", fpid);
        ret = pidWrite(filepath, writeBuf);
    }
    return ret;
}

static void subFork(int index)
{
    pid_t fpid = fork();
    if (fpid == 0) {
        sleep(5);
        exit(0);
    } else if (fpid > 0) {
        pidGroup[index] = fpid;
        return;
    }
}

static UINT32 Testcase()
{
    mode_t mode;
    int status;
    std::string testPath = "/proc/plimits/test";
    std::string maxProcPath = "/proc/plimits/test/pids.max";
    std::string plimitsProcPath = "/proc/plimits/test/plimits.procs";
    char writeBuf[10];
    int i = 0;
    pid_t fpid;
    /*创建/proc/plimits/test目录*/
    int ret = mkdir(testPath.c_str(), S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    /*并设置最大进程数为10*/
    int maxProc = 10;
    (void)memset(writeBuf, 0, sizeof(writeBuf));
    sprintf_s(writeBuf, sizeof(writeBuf), "%d", maxProc);
    ret = pidWrite(maxProcPath.c_str(), writeBuf);
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);

    /*在当前进程中创建5个子进程*/
    for (i = g_index; i < 5; ++i) {
        ret = pidForkOne(plimitsProcPath.c_str(), i);
        ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);
    }
    g_index = i;

    /*将当前进程id写入plimits.procs*/
    (void)memset(writeBuf, 0, sizeof(writeBuf));
    sprintf_s(writeBuf, sizeof(writeBuf), "%d", getpid());
    ret = pidWrite(plimitsProcPath.c_str(), writeBuf);
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);

    /*fork4个子进程*/
    for (i = g_index; i <= (g_index + 4); ++i) {
        subFork(i);
    }
    g_index = i;
    fpid = fork();
    if (fpid == 0) {
        exit(0);
    } else if (fpid > 0) {
        waitpid(fpid,&status,0);
    }

    for(i = 0; i < g_index; ++i) {
        waitpid(pidGroup[i],&status,0);
        if (0 == WEXITSTATUS(status)) //操作成功
        {
            ICUNIT_GOTO_EQUAL(0, 0, 1, EXIT);
        }
        else //操作失败
        {
            ICUNIT_GOTO_EQUAL(0, -1, -1, EXIT);
        }
    }
    /*删除/proc/plimits/test目录*/
    (void)RmdirControlFile(testPath);
    ret = rmdir(testPath.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;

EXIT:
    /*删除/proc/plimits/test目录*/
    (void)RmdirControlFile(testPath);
    (void)rmdir(testPath.c_str());
    return 0;
}

void ItProcessPlimitsPid005(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_PID_005", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}
